<template>
    <div class="login-box">
        <div class="login-form-wrap">
            <h2 class="login-tit">登录</h2>
            <el-form :model="loginForm" :rules="rules" ref="loginFormRef" size="medium" class="login-form">
                <el-form-item label="" prop="username">
                    <el-input type="text" prefix-icon="el-icon-user" placeholder="用户名" v-model="loginForm.username" autocomplete="off" clearable></el-input>
                </el-form-item>
                <el-form-item label="" prop="pass">
                    <el-input type="password" prefix-icon="el-icon-lock" placeholder="密码" v-model="loginForm.pass" autocomplete="off" clearable></el-input>
                </el-form-item>                
                <div class="login-form-btn">
                    <el-button type="primary" size="medium" @click="submitForm">登录</el-button>
                </div>
            </el-form>
        </div>
    </div>
</template>

<script>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'

export default {
    setup () {
        const router = useRouter()
        const loginFormRef = ref(null)

        const loginForm = reactive({username: '', pass: ''})
        const rules = reactive({
            username: [
                { required: true, message: '用户名不能为空', trigger: 'change' }
            ],
            pass: [
                { required: true, message: '密码不能为空', trigger: 'change' }
            ]
        })
        const submitForm = async () => {
            const {username, pass} = loginForm
            console.log(username, pass)
            loginFormRef.value.validate().then(() => {
                router.replace('/')                
            }).catch(() => {
                console.log('表单数据未通过校验')
            })
        }
        return {
            loginForm,
            loginFormRef,
            rules,
            submitForm
        }
    }
}
</script>

<style lang="scss" scoped>
.login-box {
    height: 100%;
    background: url('../assets/background.jpg') no-repeat;
    background-size: cover;
}
.login-form-wrap {
    position: absolute;
    height: 300px;
    width: 320px;
    right: 200px;
    top: 50%;
    margin-top: -150px;
    padding: 30px 20px 20px;
    box-sizing: border-box;
    background-color: #fff;
    border-radius: 8px;
    box-shadow: 0 0 4px rgb(0, 0, 0, 0.2);
    
    .login-tit {
        text-align: center;
        line-height: 30px;        
    }
    .login-form {
        margin-top: 20px;
    }
    .login-form-btn {
        margin-top: 30px;
        .el-button {
            width: 100%;
        }
    }
}
</style>